package com.sghd.football.utils.json;

import com.sghd.common.utils.json.JsonUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.SystemPropertyUtils;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wch
 * @date 2018-06-29 16:20
 * @desc 初始化json对象
 **/
@Component
public class JsonInit {
    /** 资源搜索分析器*/
    private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    /** 类的元数据读取器，由它来负责读取类上的注释信息 */
    private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
    /** 默认资源匹配符 */
    protected static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
    private final static String SCAN_PACKAGE_NAME="com.sghd.football.**.model";

    //初始始化json对象
    @PostConstruct
    public void init(){
        Object[] types = new Object[]{new Integer(0), new String(), new Boolean(false), new Long(0)
        };
        JsonUtils.object2String(types);//初始始化基本类型
        try {
            List<Resource> resources = getResource();
            String name = Json.class.getName();
            for (Resource resource : resources) {
                if (!resource.isReadable()) {
                    continue;
                }
                // 元数据
                MetadataReader metaReader = this.metadataReaderFactory.getMetadataReader(resource);
                AnnotationMetadata annoMeta = metaReader.getAnnotationMetadata();
                if (!annoMeta.hasAnnotation(name)) {
                    continue;
                }
                ClassMetadata clzMeta = metaReader.getClassMetadata();
                Object obj = Class.forName(clzMeta.getClassName()).newInstance();
                JsonUtils.object2String(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Resource> getResource() throws IOException {
        String[] pagakages = SCAN_PACKAGE_NAME.split(",");
        List<Resource> results = new ArrayList<>();
        for (String pagakage : pagakages) {
           String paga = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + resolveBasePackage(pagakage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
            Resource[] resources = this.resourcePatternResolver.getResources(paga);
            results.addAll(Arrays.asList(resources));
        }
        return results;
    }

    protected String resolveBasePackage(String basePackage) {
        return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage));
    }


}
